<!--
 * @Author: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @Date: 2022-10-21 15:17:20
 * @LastEditors: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @LastEditTime: 2022-10-25 19:50:54
 * @FilePath: \v-shop\code\src\views\goods.vue
 * @Description: goods页面
-->
<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <br>
    <br>

    <!-- 卡片 -->
    <el-card>
      <!-- 搜索框 -->
      <el-input placeholder="请输入内容" v-model="params.query" style="width:428px">
        <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
      </el-input>

      <!-- 添加按钮 -->
      <el-button type="primary" class="tj" @click="add">添加商品</el-button>
      <br>
      <br>
      <!-- 表格 -->
      <el-table :data="goods" border style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <!-- 插槽时间 -->
          <template slot-scope="scope">
            {{ scope.row.add_time | time }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="params.pagenum" :page-sizes="[1, 2, 3, 4]" :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>


    </el-card>

    <!-- 添加模态框 -->
    <el-dialog title="添加商品" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="ruleForm.goods_name"></el-input>
        </el-form-item>

        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="ruleForm.goods_price"></el-input>
        </el-form-item>

        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="ruleForm.goods_number"></el-input>
        </el-form-item>

        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="ruleForm.goods_weight"></el-input>
        </el-form-item>

        <el-form-item label="上传图片">
          <el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">添加商品</el-button>
      </div>
    </el-dialog>



    <!-- 修改模态框 -->
    <el-dialog title="修改商品" :visible.sync="dialogFormVisible2">
      <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="ruleForm2.goods_name"></el-input>
        </el-form-item>

        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="ruleForm2.goods_price"></el-input>
        </el-form-item>

        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="ruleForm2.goods_number"></el-input>
        </el-form-item>

        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="ruleForm2.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="submitForm2('ruleForm2')">修改商品</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { goods, goods_tj, goods_sc, goods_xg } from '../http/goods/api'
export default {
  name: 'goods',
  data() {
    return {
      goods: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      // 分页
      params: {
        query: "",
        pagenum: 1,
        pagesize: 4,
      },
      total: 0,
      // 添加
      ruleForm: {
        goods_name: "",
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_cat: "1,2,3",
      },
      // 修改
      ruleForm2: {
        goods_name: "",
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
      },
      // 添加验证
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
      // 修改眼证
      rules2: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    };
  },

  components: {},

  computed: {},

  // 钩子函数
  mounted() {
    this.getdata()
  },

  // 过滤时间格式
  filters: {
    time(val) {
      return new Date(val).toLocaleString()
    }
  },
  // 方法
  methods: {


    // 渲染
    getdata() {
      goods(this.params).then(res => {
        this.goods = res.data.data.goods
        this.total = res.data.data.total
      })
    },
    // 分页
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getdata()
    },
    handleCurrentChange(val) {
      this.params.pagenum = val
      this.getdata()
    },

    // 点击出现添加模态框
    add() {
      this.dialogFormVisible = true
    },

    // 添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goods_tj(this.ruleForm).then(res => {
            console.log(res, '添加');
            this.getdata();
            this.dialogFormVisible = false
            this.$message({
              type: "success",
              message: res.data.meta.msg
            })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 修改
    submitForm2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goods_xg(`goods/${this.ruleForm2.goods_id}`, this.ruleForm2).then(res => {
            console.log(res, '修改');
            this.getdata();
            this.dialogFormVisible2 = false
            this.$message({
              type: "success",
              message: res.data.meta.msg
            })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    // 编辑
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible2 = true
      this.ruleForm2 = row
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        goods_sc(row).then(res => {
          console.log(res, '删除');
          this.getdata();
          this.$message({
            type: "success",
            message: res.data.meta.msg
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },

    // 搜索
    search() {
      this.params.pagenum = 1
      this.getdata()
    }



  },
  watch: {},
  mixins: []
}

</script>
<style  scoped>
.tj {
  margin-left: 20px;
}
</style>